package 中等;

import java.util.logging.Level;

public class 二叉树展开为链表114 {

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6);
        treeNode1.left = treeNode2;
        treeNode2.left = treeNode3;
        treeNode2.right = treeNode4;
        treeNode1.right = treeNode5;
        treeNode5.right = treeNode6;
        flatten(treeNode1);


    }
    public static void flatten(TreeNode root) {
        if (root == null ){
            return;
        }
        if (root.left == null){
            flatten(root.right);
            return;
        }
        TreeNode right = root.right;
        root.right = root.left;
        root.left = null;
        flatten(root.right);
        TreeNode cur = root.right;
        while (cur.right !=null){
            cur = cur.right;
        }
        flatten(right);
        cur.right = right;
    }
}
